package com.okcl.manager.config;

import com.okcl.manager.Interceptor.LoginAuthInterceptor;
import com.okcl.manager.properties.UserProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;
    @Resource
    private UserProperties userProperties;

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("添加拦截器...");
        registry.addInterceptor(loginAuthInterceptor)
                .addPathPatterns("/**")
                //.excludePathPatterns("/admin/system/index/login", "/common/**");
                .excludePathPatterns(userProperties.getNoAuthUrls());
    }

    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("添加跨域配置...");
        registry.addMapping("/**") //添加路径规则
                .allowCredentials(true) //允许发送cookie
                .allowedOriginPatterns("*") //允许访问的源
                .allowedMethods("*") //允许的请求方式
                .allowedHeaders("*"); //允许的请求头
    }

}
